package com.cryptoneedle.center.source.dolphin.api;


import com.cryptoneedle.center.config.helper.ConfigPropertyHelper;
import com.cryptoneedle.center.source.dolphin.config.ClientResult;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class TaskDefinitionApiClient {

    private final RestTemplate restTemplate;

    public TaskDefinitionApiClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    private String base(String path) {
        String base = ConfigPropertyHelper.dsUrl();
        return path.startsWith("/") ? base + path : base + "/" + path;
    }

    public ClientResult<JsonNode> updateTaskWithUpstream(long projectCode, long code, String taskDefinitionJsonObj, String upstreamCodes) {
        String url = base("/projects/" + projectCode + "/task-definition/" + code + "/with-upstream");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
        form.add("taskDefinitionJsonObj", taskDefinitionJsonObj);
        if (upstreamCodes != null) form.add("upstreamCodes", upstreamCodes);
        ResponseEntity<ClientResult> resp = restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(form, headers), ClientResult.class);
        return resp.getBody();
    }

    public ClientResult<JsonNode> queryTaskDefinitionVersions(long projectCode, long code, int pageNo, int pageSize) {
        String url = base("/projects/" + projectCode + "/task-definition/" + code + "/versions?pageNo=" + pageNo + "&pageSize=" + pageSize);
        return restTemplate.getForObject(url, ClientResult.class);
    }

    public ClientResult<JsonNode> switchTaskDefinitionVersion(long projectCode, long code, int version) {
        String url = base("/projects/" + projectCode + "/task-definition/" + code + "/versions/" + version);
        return restTemplate.getForObject(url, ClientResult.class);
    }

    public ClientResult<JsonNode> deleteTaskDefinitionVersion(long projectCode, long code, int version) {
        String url = base("/projects/" + projectCode + "/task-definition/" + code + "/versions/" + version);
        ResponseEntity<ClientResult> resp = restTemplate.exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, ClientResult.class);
        return resp.getBody();
    }

    public ClientResult<JsonNode> queryTaskDefinitionDetail(long projectCode, long code) {
        String url = base("/projects/" + projectCode + "/task-definition/" + code);
        return restTemplate.getForObject(url, ClientResult.class);
    }

    public ClientResult<JsonNode> genTaskCodeList(long projectCode, int genNum) {
        String url = base("/projects/" + projectCode + "/task-definition/gen-task-codes?genNum=" + genNum);
        return restTemplate.getForObject(url, ClientResult.class);
    }

    public ClientResult<JsonNode> releaseTaskDefinition(long projectCode, long code, String releaseState) {
        String url = base("/projects/" + projectCode + "/task-definition/" + code + "/release");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
        form.add("releaseState", releaseState);
        return restTemplate.postForObject(url, new HttpEntity<>(form, headers), ClientResult.class);
    }
}